import { ApiProperty } from "@nestjs/swagger";
import dayjs from "dayjs";
import {  parseSecretKeyTime } from "@/config";

export class ResultData {
    constructor(code = 200, msg?: string, data?: any) {
        this.code = code;
        this.msg = msg || "ok";
        this.data = data || null;
        this.time = parseSecretKeyTime();
    }
    
    @ApiProperty({ type: "number", default: 200 })
    code: number;
    
    @ApiProperty({ type: "string", default: "ok" })
    msg?: string;
    
    data?: any;
    
    time?: string;
    
    static ok(data?: any, msg?: string): ResultData {
        return new ResultData(200, msg, data);
    }
    
    static rowOk(data?: any, msg?: string): ResultData {
        return new ResultData(200, msg, {
            row: data[0],
            total: data[1]
        });
    }
    
    static fail(code: number, msg?: string, data?: any): ResultData {
        return new ResultData(code || 500, msg || "fail", data);
    }
}
